package com.tianhui.consignortwofei.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.CheckUtil;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.SalesManInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.SalesManListAdapter;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.inter.OnItemClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fgs
 * @date 2021/6/28
 * @description
 **/
public class SalesManListFragment extends SearchFragment {

    private OrderModel mModel;
    private String mSearchKey;

    public SalesManListFragment() {
        // Required empty public constructor
    }

    @Override
    protected void changeSearchKey(String searchKey) {
        mSearchKey = searchKey;
    }

    @Override
    protected int getItemLineHeight() {
        return 1;
    }

    @Override
    public boolean isNeedLazyLoad() {
        return false;
    }

    @Override
    protected CommonView createView() {
        return null;
    }

    @Override
    protected CommonPresenter createPresenter() {
        return null;
    }

    @Override
    protected void requestSearch(String searchKey) {
        mSearchKey = searchKey;
        refreshData(true);
    }

    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new SalesManListAdapter(getContext());
    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                SalesManInfo salesManInfo = (SalesManInfo) mAdapter.getAllData().get(position);
                if (salesManInfo != null) {
                    setSelectSalesMan(salesManInfo);
                }
            }
        });
    }

    private void setSelectSalesMan(SalesManInfo salesManInfo) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(KeyValue.KEY_ID, salesManInfo.id);
        bundle.putString(KeyValue.KEY_NAME, salesManInfo.name);
        bundle.putString(KeyValue.KEY_PHONE, salesManInfo.account);
        intent.putExtras(bundle);
        getActivity().setResult(KeyValue.KEY_RESULT_CODE, intent);
        getActivity().finish();
    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        Map<String, String> map = new HashMap<>();
        if (!TextUtils.isEmpty(mSearchKey)) {
            if (CheckUtil.isNumCharacters(mSearchKey)) {
                map.put("account", mSearchKey);
            } else {
                map.put("name", mSearchKey);
            }
        }
        mModel.salesManListModel(getContext(), map, isDialog, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<SalesManInfo>>>() {
            @Override
            public void onNext(CommonResponse<List<SalesManInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    List<SalesManInfo> list = commonResponse.getData();
                    setPageInfo(1, 1);
                    if (list != null && list.size() > 0) {
                        showDataList(list);
                    } else {
                        showEmptyView();
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    @Override
    public void initData() {
        mModel = new OrderModel();
        mSearchKeyEditText.setHint("请输入姓名或手机号");
    }
}
